import { ElMessage, ElMessageBox } from 'element-plus'
import FunctionLimit from '@/views/roles/components/functionLimit.vue'
import { systemManageApi } from '@/api/modules/systemManage'
import mittBus from '@/utils/mittBus'
type RowActionType = 'functionLimit' | 'dataLimit' | 'delete'

export const handleRowAction = (type: RowActionType, rowData: any) => {
  switch (type) {
    case 'functionLimit':
      return RenderMetaFunctionLimit(rowData)
    case 'delete':
      return RenderMetadelete(rowData)
  }
}
export const handleAction = (type: string) => {
  switch (type) {
    case 'addRole':
      return AddRowFun()
  }
}
/** 编辑角色 */
const RenderMetaFunctionLimit = (rowData: any) => {
  ElMessageBox({
    title: '编辑角色',
    confirmButtonText: '提交',
    cancelButtonText: '取消',
    showCancelButton: false,
    showConfirmButton: false,
    center: true,
    customClass: 'el-message-box--fat',
    message: () => {
      return <FunctionLimit row={rowData} action="edit"></FunctionLimit>
    }
  })
    .then(() => {})
    .catch(() => {})
}
/** 删除 */
const RenderMetadelete = async (rowData: any) => {
  console.log(rowData, 'rowData')
  const res: any = await systemManageApi.delRole()
  if (res.code === 200) {
    ElMessage.success('删除成功')
    mittBus.emit('refreshRoleList')
  } else {
    ElMessage.error('删除失败')
  }
}
/** 添加角色 */
const AddRowFun = () => {
  ElMessageBox({
    title: '添加角色',
    confirmButtonText: '提交',
    cancelButtonText: '取消',
    showCancelButton: false,
    showConfirmButton: false,
    center: true,
    customClass: 'el-message-box--fat',
    message: () => {
      // return <AddRow></AddRow>
      return <FunctionLimit action="add"></FunctionLimit>
    }
  })
    .then(() => {})
    .catch(() => {})
}
/** 数组扁平化方法--针对功能点业务做了处理 */
export function flatArrFunO(arr) {
  return arr.reduce((val, item) => {
    let flatArr = [...val, item]
    if (item.children && item.level !== 2) {
      item.children.forEach(childrenItem => {
        childrenItem.parentId = item.id
      })
      flatArr = [...flatArr, ...flatArrFunO(item.children)]
    }
    const flatArrFilter = flatArr.filter(item => {
      return item.level === 2
    })
    return flatArrFilter
  }, [])
}
/** 数组扁平化方法--针对功能点业务做了处理--拿到只有三级别的数据 */
export function flatArrFunGetThird(arr) {
  return arr.reduce((val, item) => {
    let flatArr = [...val, item]
    if (item.children) {
      item.children.forEach(childrenItem => {
        childrenItem.parentId = item.id
      })
      flatArr = [...flatArr, ...flatArrFunGetThird(item.children)]
    }

    const flatArrFilter = flatArr.filter(item => {
      return item.level === 3
    })
    return flatArrFilter
  }, [])
}
/** 数组扁平化方法--公用 */
export function flatArrFun(arr) {
  return arr.reduce((val, item) => {
    let flatArr = [...val, item]
    if (item.children && item.level !== 2) {
      item.children.forEach(childrenItem => {
        childrenItem.parentId = item.id
      })
      flatArr = [...flatArr, ...flatArrFun(item.children)]
    }
    return flatArr
  }, [])
}
